package com.xuexiang.xui_lib.component.button.shadowbutton;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import com.xuexiang.xui_lib.util.AttrValue;

import org.jetbrains.annotations.Nullable;

/**
 * 基础可设置阴影效果的Button
 *
 * @author xuexiang
 * @since 2019/1/14 下午10:00
 */
public class BaseShadowButton extends Button implements Component.DrawTask, Component.EstimateSizeListener {
    public static final int SHAPE_TYPE_ROUND = 0;
    public static final int SHAPE_TYPE_RECTANGLE = 1;

    protected int mWidth;
    protected int mHeight;
    protected Paint mBackgroundPaint;
    protected int mShapeType;
    protected int mRadius;

    protected RectFloat mRectF;

    public BaseShadowButton(Context context) {
        super(context);
        init(context, null);
    }

    public BaseShadowButton(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init(context, attrSet);
    }

    public BaseShadowButton(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context, attrSet);
    }

    protected void init(final Context context, @Nullable final AttrSet attrSet) {
        Color unpressedColor = null;
        if (attrSet != null) {
            mShapeType = AttrValue.get(attrSet, "sb_shape_type", SHAPE_TYPE_RECTANGLE);
            mRadius = AttrValue.getDimension(attrSet, "sb_radius", AttrHelper.vp2px(2, context));
            unpressedColor = AttrValue.get(attrSet, "sb_color_unpressed", Color.TRANSPARENT);
        }

        mBackgroundPaint = new Paint();
        mBackgroundPaint.setStyle(Paint.Style.FILL_STYLE);
        mBackgroundPaint.setAntiAlias(true);
        //if (unpressedColor != null) {
            mBackgroundPaint.setAlpha(Color.alpha(unpressedColor.getValue()));
            mBackgroundPaint.setColor(unpressedColor);
            this.eraseOriginalBackgroundColor(unpressedColor);
        //}
        this.setClickable(true);
        addDrawTask(this, DrawTask.BETWEEN_BACKGROUND_AND_CONTENT);
        setEstimateSizeListener(this);
    }

    @Override
    public boolean onEstimateSize(int i, int i1) {
        mWidth = Component.EstimateSpec.getSize(i);
        mHeight = Component.EstimateSpec.getSize(i1);
        mRectF = new RectFloat(0, 0, mWidth, mHeight);
        return false;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (mBackgroundPaint == null) {
            return;
        }
        if (mShapeType == SHAPE_TYPE_ROUND) {
            canvas.drawCircle(mWidth / 2F, mHeight / 2F, mWidth / 2F, mBackgroundPaint);
        } else {
            canvas.drawRoundRect(mRectF, mRadius, mRadius, mBackgroundPaint);
        }
    }

    protected void eraseOriginalBackgroundColor(Color color) {
        if (color != Color.TRANSPARENT) {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(Color.TRANSPARENT.getValue()));
            this.setBackground(shapeElement);
        }
    }

    /**
     * Set the unpressed color.
     *
     * @param color the color of the background
     */
    public void setUnpressedColor(Color color) {
        mBackgroundPaint.setAlpha(Color.alpha(color.getValue()));
        mBackgroundPaint.setColor(color);
        eraseOriginalBackgroundColor(color);
        invalidate();
    }

    public int getShapeType() {
        return mShapeType;
    }

    /**
     * Set the shape type.
     *
     * @param shapeType SHAPE_TYPE_ROUND or SHAPE_TYPE_RECTANGLE
     */
    public void setShapeType(int shapeType) {
        mShapeType = shapeType;
        invalidate();
    }

    public int getRadius() {
        return mRadius;
    }

    /**
     * Set the radius if the shape type is SHAPE_TYPE_ROUND.
     *
     * @param radius by px.
     */
    public void setRadius(int radius) {
        mRadius = radius;
        invalidate();
    }
}
